package easy

func BuddyStrings(s string, goal string) bool {
	if len(s) != len(goal) {
		return false
	}
	if s == goal {
		count := make([]int, 26)
		for i := 0; i < len(s); i++ {
			count[s[i]-'a']++
			if count[s[i]-'a'] > 1 {
				return true
			}
		}
		return false
	} else {
		first := -1
		second := -1
		for i := 0; i < len(s); i++ {
			if s[i] != goal[i] {
				if first == -1 {
					first = i
				} else if second == -1 {
					second = i
				} else {
					return false
				}
			}
		}
		return (second != -1 && s[first] == goal[second] && s[second] == goal[first])
	}
}
